package com.black.app.service.impl.my;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.my.AppMyWalkDetail;
import com.black.app.domain.req.my.mydata.AppMyWalkDetailAddReq;
import com.black.app.service.my.AppMyWalkDetailService;
import com.black.app.mapper.my.AppMyWalkDetailMapper;
import com.black.common.exception.ServiceException;
import com.black.common.utils.MessageUtils;
import com.black.common.utils.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author 18494
* @description 针对表【app_my_walk_detail(今日步数详情表)】的数据库操作Service实现
* @createDate 2024-10-23 20:49:29
*/
@Service
public class AppMyWalkDetailServiceImpl extends ServiceImpl<AppMyWalkDetailMapper, AppMyWalkDetail>
    implements AppMyWalkDetailService{

    @Override
    public boolean addStep(AppMyWalkDetailAddReq req) {
        // 获取用户id
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        // 判断日期是否存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppMyWalkDetail>().eq(AppMyWalkDetail::getDate, req.getDate()))) {
            throw new ServiceException(MessageUtils.message("app.my.detail.date.exists"));
        }

        AppMyWalkDetail newEntity = new AppMyWalkDetail();
        BeanUtils.copyProperties(req, newEntity);
        boolean result = this.save(newEntity);

        return result;
    }

    @Override
    public PageInfo<AppMyWalkDetail> getWalk(AppMyWalkDetailAddReq req) {
        req.setUserId(SecurityUtils.getAppLoginUser().getUserId());
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.getWalk(req);
        });
    }
}




